package com.gucheng.springframework.beans.factory.support;

import com.gucheng.springframework.beans.BeansException;
import com.gucheng.springframework.beans.factory.config.BeanDefinition;

import java.util.HashMap;
import java.util.Map;

public class DefaultListableBeanFactory extends  AbstractAutowireCapableBeanFactory implements  BeanDefinitionRegistry{

    //beanDefinitionMap用来保存bean定义，beanName作为key
    private Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>();


    //将Bean定义注册到beanDefinitionMap中
    @Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
       beanDefinitionMap.put(beanName, beanDefinition);
    }

    //根据beanName从beanDefinitionMap中获取Bean定义
    @Override
    protected BeanDefinition getBeanDefinition(String beanName) throws BeansException {
        BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
        if (beanDefinition == null) throw new BeansException("No bean named '" + beanName + "' is defined");
        return beanDefinition;
    }
}
